<?php
class CategoryController extends Controller{
	private $db;
	private $category;
	//构造函数
	public function __init(){
		$this->db=K('Category');
		//获得所有栏目
		$this->category=$this->db->getlist();
	}
	//显示栏目列表
	public function index(){
		$this->assign('data',$this->category);
		$this->display();
	}
	//添加栏目
	public function add(){
		if(IS_POST){
			if($this->db->addCategory()){
				$this->success('操作成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$this->assign('category',$this->category);
			$this->display();
		}
	}
	//编辑栏目
	public function edit()
	{
		if (IS_POST) {
			if ($this->db->editCategory()) {
				$this->success('编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$cid=Q('get.cid',0,'intval');
			//获得当前栏目数据并分配
			$filed=$this->db->find($cid);
			$this->assign('filed',$filed);
			$category=$this->category;
			foreach ($category as $k => $v) {
				//给父栏目添加select
				$category[$k]['selected']=($v['cid']==$filed['pid'])?" selected=''":'';
				//子栏目设置disabled 自身也不能选择
				$category[$k]['disabled']=Data::isChild($category,$v['cid'],$filed['cid']) || $v['cid']==$filed['cid']?
				" disabled=''":'';
			}
			$this->assign('category',$category);
			$this->display();
		}
	}
	//删除栏目
	public function delete()
	{
		$cid=Q('cid',0,'intval');//$_REQUEST  cookie post  get 
		if ($this->db->deleteCategory($cid)) {
			$this->success('删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}
}